/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */

import { globalData, hvigor, TraceManager } from '@ohos/hvigor';

import { ModuleData, OhosTraceData, RestoolCompressionTrace, TraceIncrement } from './type.js';

class OhosTrace {
  public static readonly TRACE_KEY = 'HVIGOR_OHOS_PLUGIN';
  private data: OhosTraceData;

  constructor() {
    this.data = {
      MODULES: [],
      NATIVE_COMPILER: 'Default'
    };

    hvigor.buildFinished(() => {
      ohosTrace.transmitDataToManager();
    });
  }

  /**
   * 传输数据给打点管理中心并且在落盘后清理数据
   */
  public transmitDataToManager(): void {
    TraceManager.trace(OhosTrace.TRACE_KEY, this.data, () => {
      this.data?.MODULES?.forEach((module) => {
        delete module.RESTOOL_COMPRESSION;
        delete module.INCREMENTAL_TASKS;
      });
    });
  }

  public traceBuildMode(buildMode: string): void {
    const prop = globalData.cliEnv.configProps?.get('prop');
    if (prop) {
      this.data.BUILD_MODE = prop.get('buildMode') ?? buildMode;
    }
  }

  public traceNativeCompiler(compiler?: string) {
    this.data.NATIVE_COMPILER = compiler ?? 'Default';
  }

  /**
   * 以替换的方式记录模块数据
   * @param {string} moduleName
   * @param {string} apiType
   */
  public traceModules(moduleName: string, apiType?: string): void {
    const moduleNameAlias = TraceManager.anonymize(moduleName);
    const arr = this.data.MODULES.filter((item) => item.MODULE_NAME !== moduleNameAlias);
    arr.push({ MODULE_NAME: moduleNameAlias, API_TYPE: apiType });
    this.data.MODULES = arr;
  }

  /**
   * 获取打点数据里的模块数据
   * @param {string} moduleName
   * @returns {ModuleData | undefined}
   * @private
   */
  private getModule(moduleName: string | undefined): ModuleData | undefined {
    if (!moduleName) return undefined;
    const moduleNameAlias = TraceManager.anonymize(moduleName);
    return this.data.MODULES.find((item) => item.MODULE_NAME === moduleNameAlias);
  }

  public traceRestoolCompression(moduleName: string, restoolCompressionReport: RestoolCompressionTrace): void {
    const module = this.getModule(moduleName);
    if (module) {
      module.RESTOOL_COMPRESSION = restoolCompressionReport;
    }
  }

  public traceIncrement(moduleName: string, field: TraceIncrement, hasIncrement: boolean): void {
    const module = this.getModule(moduleName);
    if (module) {
      module.INCREMENTAL_TASKS = module.INCREMENTAL_TASKS ?? ({} as Record<TraceIncrement, boolean>);
      module.INCREMENTAL_TASKS[field] = hasIncrement;
    }
  }

  public traceIsUseCompilePlugin(moduleName: string | undefined): void {
    const module = this.getModule(moduleName);
    if (module) {
      module.IS_USE_COMPILE_PLUGIN = true;
    }
  }

  public traceIsUseTransformLib(moduleName: string | undefined): void {
    const module = this.getModule(moduleName);
    if (module) {
      module.IS_USE_TRANSFORMLIB = true;
    }
  }

  public traceUseNormalizedOHMUrl(useNormalizedOHMUrl?: boolean): void {
    this.data.USE_NORMALIZED_OHMURL = useNormalizedOHMUrl;
  }

  public traceIsAutoLazyImport(moduleName: string | undefined, isAutoLazyImport?: boolean): void {
    const module = this.getModule(moduleName);
    if (module) {
      module.IS_AUTO_LAZY_IMPORT = isAutoLazyImport;
    }
  }

  public traceIsUsePacJson(moduleName: string | undefined): void {
    const module = this.getModule(moduleName);
    if (module) {
      module.IS_USE_PAC = true;
    }
  }
}

export const ohosTrace = new OhosTrace();

